<template>
  <div class="container">
    <!-- @mouseleave="mouseleaveHandle" -->
    <div class="banner" @mouseleave="mouseleaveHandle">
      <div class="category">
        <ul>
          <li
            v-for="(item, index) in productClassify"
            :class="index === current ? 'bg-color colorfff' : ''"
            @mouseenter="enterClassify(index)"
            :key="index"
          >
            <i class="iconfont" :class="item.icon"></i>
            <span>{{ item.translateName }}</span>
          </li>
        </ul>
      </div>
      <!-- v-show="seen && categoryCurrent" -->
      <div
        v-show="seen && categoryCurrent"
        class="classifyItem"
        :key="updateKey"
      >
        <classifyCard @goCate="goCate" :list="categoryCurrent" />
      </div>

      <div class="slider-banner">
        <el-carousel height="460px">
          <el-carousel-item v-for="item in bannerList" :key="item.id">
            <el-image
              class="img"
              fit="cover"
              :src="item.pic"
              @click="linkTo(item.field101)"
            >
              <div slot="placeholder" class="image-slot">
                loading<span class="dot">...</span>
              </div>
            </el-image>
            <div class="box"></div>
          </el-carousel-item>
        </el-carousel>
      </div>
    </div>
    <div class="actList" v-for="item in actList" :key="item.id">
      <recommended v-if="item.type === 1" :activityData="item"></recommended>
      <promotions v-if="item.type === 2" :activityData="item"></promotions>
      <first-new v-if="item.type === 3" :activityData="item"></first-new>
    </div>
    <!-- 店铺街 -->
    <div class="brandstore">
      <div class="item1">
        <div class="item1-top" @click="gopage()">
          <div class="title">
            <i class="icon iconfont2 ic-dianpujie"></i>
            {{ $t(`page.index.shopStreet`) }}
          </div>
          <span class="more iconfont icon-gengduo"></span>
        </div>
        <div class="item1-bottom">
          <div class="item1-bottom-left">
            <el-carousel height="260px" indicator-position="none">
              <el-carousel-item v-for="(item, index) in storeList" :key="index">
                <div style="height: 3px">
                  <div>
                    <el-image
                      class="backImg img"
                      fit="cover"
                      :src="item.backImage"
                      @click="goMerchant(item)"
                    >
                      <div slot="placeholder" class="image-slot">
                        loading<span class="dot">...</span>
                      </div>
                    </el-image>
                  </div>
                  <div class="logo">
                    <el-image class="img" fit="scale-down" :src="item.avatar">
                      <div slot="placeholder" class="image-slot">
                        loading<span class="dot">...</span>
                      </div>
                    </el-image>
                  </div>
                </div>
              </el-carousel-item>
            </el-carousel>
          </div>
          <div class="item1-bottom-right">
            <div
              @click="goMerchant(item)"
              class="logo"
              v-for="(item, index) in storeList.slice(0, 4)"
              :key="index"
            >
              <el-image class="img" fit="scale-down" :src="item.avatar">
                <div slot="placeholder" class="image-slot">
                  loading<span class="dot">...</span>
                </div>
              </el-image>
            </div>
          </div>
        </div>
      </div>
      <!-- TOP ONE -->
      <div class="item2">
        <div class="item2-top" @click="goTopone()">
          <div class="title">
            <i class="icon iconfont2 ic-paihangbang"></i
            >{{ $t(`page.index.topOne`) }}
          </div>
          <span class="more iconfont icon-gengduo"></span>
        </div>
        <div class="item2-bottom">
          <div
            class="goods"
            v-for="(item, index) in toponeList"
            :key="index"
            @click="goDetail(item)"
          >
            <img class="pic" :src="topImg[index]" alt="" />
            <el-image class="img" fit="scale-down" :src="item.image">
              <div slot="placeholder" class="image-slot">
                loading<span class="dot">...</span>
              </div>
            </el-image>
            <div class="storeName line1">
              {{ item.storeName }}
            </div>
            <span class="price">
              <statistic :value="item.price" :currency="item.currency" />
            </span>
          </div>
        </div>
      </div>
    </div>

    <!-- 活动列表 -->
    <div
      class="activityindexList"
      v-for="item in activityindexList"
      :key="item.id"
    >
      <recommended v-if="item.type === 1" :activityData="item"></recommended>
      <promotions v-if="item.type === 2" :activityData="item"></promotions>
      <first-new v-if="item.type === 3" :activityData="item"></first-new>
    </div>
    <!-- 轮播广告 -->
    <div class="ad" v-if="adList && adList.length > 0">
      <el-carousel width="100%" height="212px" indicator-position="none">
        <el-carousel-item v-for="item in adList" :key="item.id">
          <el-image class="img" fit="cover" :src="item.pic">
            <div slot="placeholder" class="image-slot">
              loading<span class="dot">...</span>
            </div>
          </el-image>
        </el-carousel-item>
      </el-carousel>
    </div>

    <div class="product">
      <div class="product-top">
        <span class="title"
          ><i class="icon iconfont2 ic-tuijian"></i
          >{{ $t(`page.index.titAlso`) }}
        </span>
      </div>
      <div class="product-bottom">
        <div class="product-bottom">
          <product :productList="productList.slice(0, 10)"></product>
        </div>
      </div>
    </div>

    <div
      class="product"
      style="padding-top: 24px"
      v-if="productList.length > 10"
    >
      <div class="product-bottom">
        <product :productList="productList.slice(10)"></product>
      </div>
    </div>

    <!-- 加载更多 -->
    <!-- <div class="loadingicon acea-row row-center-wrapper" v-if="productList.length && productList.length >= limits">
      <span class="loading iconfont icon-jiazai" v-if="!pullRefreshss"></span>
    </div> -->
    <FloatWindow></FloatWindow>
  </div>
</template>

<script>
import recommended from "@/components/recommended.vue";
import promotions from "@/components/promotions.vue";
import firstNew from "@/components/first_new.vue";
import { goMerchant, goShopDetail } from "@/utils/order.js";
import product from "@/components/product.vue";
import classifyCard from "@/components/classify.vue";
import { isJSON, deepClone, mapToLang } from "@/utils/index.js";
import { mapGetters } from "vuex";
export default {
  name: "IndexPage",
  auth: false,
  components: {
    recommended,
    promotions,
    firstNew,
    product,
    classifyCard,
  },
  data() {
    return {
      seen: false,
      activityindexList: [],
      storeList: [],
      bannerList: [],
      colors: ["#99A9BF", "#00785E", "#00785E"],
      visible: false,
      recommendList: [],
      productList: [],
      adList: [],
      goodsList: [],
      MerTypeList: [],
      categoryCurrent: [],
      toponeList: [],
      list: [],
      actList: [],
      pullRefreshss: true,
      page: 1, //代表页面的初始页数
      limit: 10,
      limits: 50,
      scollY: null, // 离底部距离有多少
      pageTotal: 0, //总页数
      topImg: [
        require("@/assets/images/top-list0.png"),
        require("@/assets/images/top-list1.png"),
        require("@/assets/images/top-list2.png"),
      ],
      updateKey: 0,
      current: -1,
    };
  },
  async asyncData({ app, error, store }) {
    let [
      list,
      storeList,
      activityindexList,
      toponeList,
      // proList
    ] = await Promise.all([
      app.$axios.get("/api/pc/home/index"),
      app.$axios.get("/api/front/merchant/street"),
      app.$axios.get("/api/front/activity/index/list"),
      app.$axios.get("/api/front/product/leaderboard"),
      // app.$axios.get("/api/front/index/product", {
      //   params: {
      //     page: 1,
      //     limit: 20
      //   }
      // }),
    ]);

    // let relIcon = document.head.querySelector("link[rel='icon']")
    //     relIcon.href = list.data.webSiteFavicon
    localStorage.setItem("homeDataPc", JSON.stringify(list.data));
    return {
      list: list.data,
      bannerList: list.data.banner,
      adList: list.data.bastBanner,
      storeList: storeList.data.list,
      activityindexList: activityindexList.data,
      actList: activityindexList.data.splice(0, 1),
      toponeList: toponeList.data.splice(0, 3),
      // proList: proList.data.list
    };
  },
  beforeDestroy() {
    window.onscroll = null;
  },
  beforeMount() {
    this.getList();
  },
  mounted() {
    this.$nextTick(() => {
      this.$nuxt.$loading.start();
      setTimeout(() => this.$nuxt.$loading.finish(), 1000);
    });
    this.$cookies.set("merPlatChatConfig", {
      serviceEmail: this.list.consumerEmail,
      serviceLink: this.list.consumerH5Url,
      servicePhone: this.list.consumerHotline,
      serviceMessage: this.list.consumerMessage,
      serviceType: this.list.consumerType,
    });
    this.pullRefresh();
  },
  computed: {
    locale() {
      console.log("this.$i18n.locale", mapToLang(this.$i18n.locale));
      return mapToLang(this.$i18n.locale);
    },
    ...mapGetters(["translateNameProductClassify"]),
    productClassify() {
      return this.translateNameProductClassify;
    },
  },
  watch: {
    productClassify: {
      handler(val) {
        this.updateKey++;
      },
      deep: true,
    },
  },
  methods: {
    // 商品分类
    goCate(items) {
      // this.cid = items.id;
      this.$router.push({
        path: "/goods/goods_search",
        query: {
          cid: items.id,
          // title: this.search ? this.search.trim() : "",
        },
      });
    },
    enterClassify(index) {
      this.seen = true;
      this.current = index;
      if (this.productClassify[index].childList) {
        this.categoryCurrent = this.productClassify[index].childList;
      }
    },
    toTranslateName(list) {
      if (Array.isArray(list)) {
        list.forEach((item) => {
          if (isJSON(item.name)) {
            item.translateName = JSON.parse(item.name)[this.locale];
          } else {
            item.translateName = item.name;
          }
        });
      }
    },
    goDetail(item) {
      goShopDetail(item.id, this);
    },
    goTopone() {
      this.$router.push({
        path: "/goods/top_one",
      });
    },
    watchScroll() {
      let _this = this;
      this.scollY =
        this.comsys.getScrollTop() +
        this.comsys.getWindowHeight() -
        this.comsys.getScrollHeight();
      // 把下拉刷新置为false，防止多次请求
      if (this.scollY >= -50) {
        if (this.page >= Math.ceil(this.pageTotal / this.limits)) {
          this.pullRefreshss = true;
          return false;
        }
        if (!this.pullRefreshss) {
          return false;
        }
        _this.pullRefreshss = false;
        if (_this.productList.length < _this.pageTotal) {
          // 加页码数
          _this.page++;
          _this.getList();
        }
      } else {
        // 其他时候把下拉刷新置为true
        _this.pullRefreshss = true;
      }
    },
    getList() {
      let currentPage = { page: this.page, limit: this.limits };
      this.$axios
        .get(`/api/front/index/product`, {
          params: currentPage,
        })
        .then((res) => {
          this.pageTotal = res.data.total;
          let proList = [];
          if (this.page === 1 && res.data.list.length > 2) {
            let top2 = res.data.list.slice(0, 2);
            let others = this.shuffle(res.data.list.slice(2));
            proList = [...top2, ...others];
          } else {
            proList = this.shuffle(res.data.list);
          }
          this.productList = this.productList.concat(proList);
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },

    // 洗牌算法
    shuffle(arr) {
      for (let i = arr.length; i; i--) {
        // 产生一个随机位置
        let j = Math.floor(Math.random() * i);

        // 交换位置
        [arr[i - 1], arr[j]] = [arr[j], arr[i - 1]];
      }
      return arr;
    },
    gopage() {
      this.$router.push({
        path: "/merchant/merchant_street",
      });
    },
    goMerchant(item) {
      goMerchant(item.id, this);
    },
    // 下拉加载ajax
    pullRefresh: function () {
      let _this = this;
      window.onscroll = function () {
        _this.watchScroll();
      };
    },
    linkTo(path, query) {
      if (path) {
        window.open(path);
      }
    },
    mouseleaveHandle() {
      this.seen = false;
      this.categoryCurrent = [];
      this.index = -1;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/css/variables.scss";

.container {
  width: 100%;
  flex: 1;

  .banner {
    width: 1200px;
    // height: 488px;
    margin: auto;
    position: relative;
    background-color: #fff;
    padding: 12px;
    border-radius: 20px;
    display: flex;
    margin-top: 24px;
    position: relative;

    .classifyItem {
      height: 460px;
      width: calc(100% - 274px);
      z-index: 9;
      position: absolute;
      top: 12px;
      left: 274px;
      padding: 16px;
      background-color: #fff;
    }

    .category {
      width: 258px;
      height: 460px;
      overflow: scroll;
      background-color: $use-color-bg;
      padding: 8px;
      padding-right: 16px;
      box-sizing: border-box;
      border-radius: 16px;

      li {
        min-height: 50px;
        border-radius: 6px;
        padding: 14px 16px;

        &:hover {
          background-color: $user--color-primary;
          color: #fff;
        }
      }
    }

    .slider-banner {
      width: 100%;
      flex: 1;
      margin: auto;
      border-radius: 16px;
      margin-left: 6px;
      // ::v-deep .el-carousel__arrow--left {
      //   left: 224px;
      // }
    }

    .nav-tabs {
      width: 260px;
      height: 480px;
      background: #242424;
      opacity: 0.85;
      position: absolute;
      top: 0;
      left: 360px;
      z-index: 99;
    }

    .img {
      width: 100%;
      height: 100%;
    }
  }

  .brandstore {
    background-color: #fff;
    padding: 12px;
    border-radius: 20px;
    width: 1200px;
    margin: auto;
    display: flex;
    justify-content: space-between;
    margin-top: 16px;

    > div {
      border-radius: 8px;
      background: $use-color-bg;
      height: 335px;
    }

    .title {
      font-size: 22px;
      font-family: PingFang SC, Arial-BoldMT, Arial;
      line-height: 20px;
      margin: 20px 0 43px 20px;
      font-weight: 500;

      .icon {
        font-size: 26px;
        color: $user--color-primary;
        margin-right: 11px;
      }
    }

    .item1 {
      width: 580px;
      display: flex;
      flex-wrap: wrap;
      margin-right: 12px;

      .item1-top {
        width: 100%;
        height: 59px;
        color: $use-color-info;
        display: flex;
        justify-content: space-between;
        cursor: pointer;

        .iconfont {
          margin-top: 20px;
          margin-right: 20px;
        }
      }

      .item1-bottom {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        margin: 0 14px 16px 16px;

        .item1-bottom-left {
          width: 260px;
          border-radius: 8px;
          margin-right: 14px;
          cursor: pointer;

          .logo {
            width: 241px;
            height: 64px;
            margin-top: 10px;
            display: flex;
            justify-content: center;

            .img {
              width: 96px;
              height: 48px;
            }
          }

          .backImg {
            width: 260px;
            height: 260px;
            display: inline-block !important;
            background-color: #fff;
            border-radius: 8px;
          }
        }

        .item1-bottom-right {
          flex: 1;
          overflow: hidden;
          display: grid;
          grid-column-gap: 14px;
          grid-row-gap: 10px;
          grid-template-columns: auto auto;

          .logo {
            width: 130px;
            height: 125px;
            border-radius: 8px;
            background-color: #fff;

            .img {
              width: 100%;
              height: 100%;
            }
          }
        }
      }
    }

    .item2 {
      width: 580px;
      display: flex;
      flex-wrap: wrap;

      .item2-top {
        width: 100%;
        height: 59px;
        color: $use-color-info;
        display: flex;
        justify-content: space-between;
        cursor: pointer;

        .iconfont {
          margin-top: 20px;
          margin-right: 20px;
        }

        .more {
          font-size: 16px;
          font-family: ArialMT;
          line-height: 16px;
          margin-left: 71px;
        }
      }

      .item2-bottom {
        width: 100%;
        height: 250px;
        padding: 0 16px;
        display: grid;
        grid-column-gap: 12px;
        grid-template-columns: repeat(3, 1fr);

        .pic {
          position: absolute;
          width: 28px;
          height: 31px;
          z-index: 98;
          margin-left: 6px;
          top: -3px;
        }

        .goods {
          width: 176px;
          height: 250px;
          cursor: pointer;
          position: relative;
          background-color: #fff;
          border-radius: 8px;
          padding: 8px;

          .img {
            width: 160px;
            height: 160px;
            border-radius: 8px;
            background-color: #fff;
          }

          .storeName {
            width: 100%;
            height: 24px;
            font-size: 16px;
            color: $use-color-info-light-2;
            line-height: 24px;
            margin-top: 14px;
          }

          .price {
            font-size: 16px;
            font-weight: 900;
            color: $user--color-price;
            line-height: 24px;
          }
        }
      }
    }

    .more {
      font-size: 16px;
      font-family: ArialMT;
      line-height: 16px;
      margin-left: 71px;
      color: $use-color-info-light-4;
    }
  }

  .product {
    width: 1200px;
    border-radius: 8px;
    background-color: #fff;
    margin: 16px auto 0;
    padding: 0 24px 24px;

    .product-top {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .title {
        font-size: 20px;
        text-align: center;
        line-height: 68px;
        font-weight: 500;

        .icon {
          font-size: 26px;
          color: $user--color-primary;
          margin-right: 10px;
        }
      }

      .more {
        font-size: 14px;
        text-align: center;
        float: right;
        color: #666;
        margin-right: 20px;
      }
    }

    .product-bottom {
      display: flex;
      flex-wrap: wrap;

      .brandshop {
        display: flex;
      }

      .item1 {
        width: 226px;
        height: 675px;
        background: #fff;
        margin: 0 15px 15px 0;
        float: left;

        .img {
          width: 226px;
          height: 675px;
        }
      }

      .goods_item {
        display: inline-block;
        height: 330px;
        background: #ffffff;
        cursor: pointer;

        .img {
          width: 218px;
          height: 218px;
        }

        .goods_content {
          .store_name {
            height: 44px;
            margin: 0 22px 20px 15px;
            font-size: 16px;
            font-family: ArialMT;
            color: #333333;
            line-height: 22px;
          }

          .lines2 {
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
          }
        }

        .acea_row {
          .price {
            font-size: 18px;
            font-family: DINCond-Black, DINCond;
            font-weight: 900;
            color: #00785e;
            line-height: 15px;
            margin-left: 15px;
            float: left;
          }

          .ot_price {
            font-size: 14px;
            color: $use-color-info-light-3;
          }
        }
      }
    }

    .bottom {
      margin-top: 14px;
    }
  }

  .ad {
    width: 1200px;
    height: 212px;
    margin: auto;
    margin-top: 16px;
    border-radius: 8px;
    overflow: hidden;

    .img {
      width: 100%;
      height: 100%;
    }
  }

  .el-carousel__item.is-animating {
    display: flex;
  }

  .wrapper {
    display: grid;
    grid-column-gap: 16px;
    grid-row-gap: 24px;
    grid-template-columns: repeat(5, 1fr);

    .goods_item {
      display: inline-block;
      background: #ffffff;

      &:hover {
        .img {
          box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.2);
        }
      }

      .img {
        margin-bottom: 12px;
        border: 1px solid $use-color-line;
        border-radius: 8px;
      }

      .goods_content {
        .store_name {
          height: 44px;
          margin: 0 22px 13px 15px;
          font-size: 16px;
          color: #333333;
          line-height: 22px;
        }

        .store_name1 {
          height: 48px;
          font-size: 16px;
          color: #333333;
          line-height: 24px;
        }

        .lines2 {
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
      }

      .acea_row1 {
        display: flex;
      }

      .acea_row {
        padding-top: 6px;
        justify-content: space-between;
        align-items: center;
        display: flex;

        .price1 {
          font-size: 20px;
          font-weight: 900;
          color: $user--color-price;
          line-height: 20px;
          margin-bottom: 9px;
          display: inline-block;

          .sold {
            font-size: 12px;
            color: #999999;
            margin-right: 15px;
          }
        }

        .ot_price {
          font-size: 12px;
          font-weight: 400;
          color: #888888;
          line-height: 12px;
          display: flex;
          justify-content: space-between;

          .lines {
            text-decoration: line-through;
          }
        }

        .ot_price1 {
          font-size: 12px;
          font-weight: 400;
          color: #999999;
          line-height: 29px;

          .lines {
            text-decoration: line-through;
          }
        }
      }
    }
  }
}

.flex-column {
  display: flex;
  flex-direction: column;
}

.goods_item:nth-child(4n) {
  margin-right: 0 !important;
}

.topindeximg1 {
  width: 54px;
  height: 66px;
  position: absolute;
  left: 900px;
  background-image: url("../assets/images/01.png");
}

.topindeximg2 {
  width: 54px;
  height: 66px;
  position: absolute;
  left: 1090px;
  background-image: url("../assets/images/02.png");
}

.topindeximg3 {
  width: 54px;
  height: 66px;
  position: absolute;
  left: 1280px;
  background-image: url("../assets/images/03.png");
}

.actList {
  margin: 16px auto 0;
  width: 1200px;
  border-radius: 8px;
  overflow: hidden;
}

.activityindexList {
  margin: 16px auto 0;
}
.colorfff {
  color: #fff;
}
</style>
